import 'dart:async';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../common/constants.dart';

class HFontSizes {
  static const normal = 16.0;
  static const normal2 = 15.0;
  static const small = 14.0;
  static const smaller = 13.0;
}

extension FontWeightExt on FontWeight {
  static const FontWeight light = FontWeight.w300;
  static const FontWeight regular = FontWeight.w400;
  static FontWeight medium = Platform.isAndroid ? FontWeight.w600 : FontWeight.w600;
  static const FontWeight semibold = FontWeight.w800;
  static const FontWeight bold = FontWeight.w900;
}

class HColors {
  static const Color contentBackgroundColor = Colors.white;
  static const Color contentBackgroundGrayColor2 = Color(0xFFF8F8F8);
  static const Color secondaryLabelColor2 = Color(0xFF666666);
  static const Color secondaryLabelColor3 = Color(0xFF999999);
  static const Color secondaryLabelColor4 = Color(0xFFB4B4B4);
  static const Color blue2 = Color(0xFF0091FF);
  static const Color primary = Color(0xFF448AFF);
  static const Color secondary = Color(0xFFE95E3D);
  static const Color mediumGrey = Color(0xFF86919C);
}

class HDialog {
  static Future<T?> show<T>(
      {required BuildContext context, String title = '温馨提示', String? content, bool barrierDismissible = true, Color? barrierColor = Colors.black54, bool? useSafeArea, bool? useRootNavigator, Widget? contentWidget, List<DialogAction> options = const [DialogAction(text: '知道了')]}) {
    return showDialog(
      context: context,
      barrierDismissible: barrierDismissible,
      barrierColor: barrierColor,
      useSafeArea: useSafeArea ?? true,
      useRootNavigator: useRootNavigator ?? true,
      builder: (BuildContext context) => HDialogWidget(
        title: title,
        content: content,
        contentWidget: contentWidget,
        options: options,
        isSheetStyle: false,
      ),
    );
  }

  static showCustom({required String title, required String content, required String confirmTitle, required String cancelTitle, required Function confirmAction, required Function cancelAction}) {
    Get.defaultDialog(
      barrierDismissible: false,
      title: title,
      titleStyle: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
      titlePadding: const EdgeInsets.only(top: 16, bottom: 10),
      content: Text(
        content,
        textAlign: TextAlign.center,
        style: TextStyle(color: Colors.black54),
      ),
      contentPadding: const EdgeInsets.fromLTRB(16, 0, 16, 5),
      confirm: Container(
        width: 120,
        margin: const EdgeInsets.only(bottom: 16),
        child: FilledButton(
          onPressed: () {
            Get.back();
            confirmAction();
          },
          style: FilledButton.styleFrom(
            backgroundColor: Constants.themeColor,
            padding: const EdgeInsets.symmetric(horizontal: 8),
          ),
          child: Text(
            confirmTitle,
            style: TextStyle(color: Colors.white, fontSize: 14),
          ),
        ),
      ),
      cancel: Container(
        width: 120,
        margin: const EdgeInsets.only(bottom: 16),
        child: FilledButton(
          onPressed: () {
            Get.back();
            cancelAction();
          },
          style: FilledButton.styleFrom(
            backgroundColor: Colors.grey[300],
            padding: const EdgeInsets.symmetric(horizontal: 8),
          ),
          child: Text(
            cancelTitle,
            style: TextStyle(color: Colors.black, fontSize: 14),
          ),
        ),
      ),
    );
  }
}

class DialogAction<T> {
  final String? text;
  final ActionType type;
  final Widget? child;
  final T? actionValue;
  final VoidCallback? onPressed;

  const DialogAction({this.text, this.type = ActionType.positive, this.child, this.actionValue, this.onPressed});
}

enum ActionType { delete, positive, negative }

Color _getActionColor(ActionType type) {
  switch (type) {
    case ActionType.delete:
      return HColors.secondary;
    case ActionType.negative:
      return HColors.mediumGrey;
    case ActionType.positive:
      return HColors.primary;
    default:
      return HColors.primary;
  }
}

class HDialogWidget<T> extends StatelessWidget {
  final String? title;
  final String? content;
  final Widget? contentWidget;
  final List<DialogAction<T>> options;
  final DialogAction<T>? bottomSheetCancel;
  final bool isSheetStyle;

  const HDialogWidget({Key? key, this.title, this.content, this.contentWidget, this.options = const [], this.bottomSheetCancel, this.isSheetStyle = false}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (Platform.isIOS) {
      return createIOSDialog(content: content, contentWidget: contentWidget, title: title, context: context, options: options);
    } else {
      return createAndroidDialog(content: content, contentWidget: contentWidget, title: title, context: context, options: options);
    }
  }

  AlertDialog createAndroidDialog<T>({required BuildContext context, String? title, String? content, required List<DialogAction<T>> options, Widget? contentWidget}) {
    final actions = options.map((option) {
      return CupertinoButton(
          onPressed: () {
            Navigator.pop(context, option.actionValue);
            if (option.onPressed != null) option.onPressed!();
          },
          child: option.child ?? Text(option.text ?? '', style: TextStyle(color: _getActionColor(option.type))));
    }).toList();
    final dialog = AlertDialog(
      title: title == null ? null : Text(title),
      content: contentWidget != null && content == null ? contentWidget : Text(content ?? ''),
      actions: actions,
    );
    return dialog;
  }

  Widget createIOSDialog<T>({required BuildContext context, String? title, String? content, Widget? contentWidget, required List<DialogAction<T>> options}) {
    final actions = options.map((option) {
      return CupertinoButton(
        onPressed: () {
          Navigator.pop(context, option.actionValue);
          if (option.onPressed != null) option.onPressed!();
        },
        child: option.child ?? Text(option.text ?? '', style: TextStyle(color: _getActionColor(option.type))),
      );
    }).toList();
    return CupertinoAlertDialog(
      title: title == null ? null : Text(title),
      content: contentWidget != null && content == null ? contentWidget : Text(content ?? ''),
      actions: actions,
    );
  }

  Widget createIOSSheetDialog<T>({required BuildContext context, String? title, String? content, Widget? contentWidget, required List<DialogAction<T>> options, DialogAction<T>? bottomSheetCancel}) {
    final actions = options.map((option) {
      return CupertinoActionSheetAction(
          onPressed: () {
            Navigator.pop(context, option.actionValue);
            if (option.onPressed != null) option.onPressed!();
          },
          child: option.child ?? Text(option.text ?? '', style: TextStyle(color: _getActionColor(option.type))));
    }).toList();
    final cancelButton = bottomSheetCancel == null
        ? null
        : CupertinoActionSheetAction(
            onPressed: () {
              Navigator.pop(context, bottomSheetCancel.actionValue);
              if (bottomSheetCancel.onPressed != null) bottomSheetCancel.onPressed!();
            },
            isDefaultAction: true,
            child: bottomSheetCancel.child ?? Text(bottomSheetCancel.text ?? '', style: TextStyle(color: _getActionColor(bottomSheetCancel.type))),
          );
    return CupertinoActionSheet(title: title == null ? null : Text(title), message: contentWidget != null && content == null ? contentWidget : Text(content ?? ''), actions: actions, cancelButton: cancelButton);
  }
}
